# -*- coding:utf-8 -*-


from rest_framework import serializers
from download.models import Download, Restype


# class ChipSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Chip
#         fields = "__all__"


class DownloadSerializer(serializers.ModelSerializer):
    class Meta:
        model = Download
        fields = "__all__"
        
    chip = serializers.SerializerMethodField()
    category = serializers.SerializerMethodField() 
     
    def get_chip(self, obj):
#         chip = obj.chip.all()
#         chip_ser = ChipSerializer(chip, many=True)
#         return chip_ser.data
        temp = []
        for item in obj.chip.all().order_by('name'):
            temp.append(item.name)
        return temp
    
    def get_category(self, obj):
        if obj.category is not None:
            return obj.category.name

  
class RestypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Restype
        fields = "__all__"
        
    def to_representation(self, item):
        data = super().to_representation(item)
        data['value'] = str(data['id'])
        data['text'] = str(data['name'])
        date_copy = data.copy()
        for key,value in data.items():
            if not value:
                date_copy.pop(key)
        return date_copy


        
        
        

